package bittorrent;

import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.util.ArrayList;

public class Response {
    /* const */
        public static final String FAILURE_REASON = "failure_reason";
        public static final String WARNING_MESSAGE = "warning_message";
        public static final String INTERVAL = "interval";
        public static final String COMPLETE = "complete";
        public static final String INCOMPLETE = "incomplete";
        public static final String PEERS = "peers";

    /* atribut - atribut yang dimiliki oleh Response */
        private String failure_reason = ""; /* value dari pesan error mengapa request gagal */
        private String warning_message = "";/* value dari pesan peringatan */
        private int interval;               /* interval (dalam detik) dimana client harus mengirimkan lagi request ke tracker */
        private int complete;               /* jumlah client yang memiliki file utuh, disebut sebagai seeders */
        private int incomplete;             /* jumlah client non-seeders, disebut sebagai leechers */
        private ArrayList < Peer > peers;
            /* list dari ip dan port peer dalam format String:int */
    
    /* constructor */
        public Response (String teks) throws UnsupportedEncodingException {
            String[] responseLine = teks.split("\n");
            String[] responseToken;
            String[] responseValue;
            int activeLine = 0;
            if (responseLine[0].indexOf(FAILURE_REASON) != -1) {
                responseToken = responseLine[0].split(" ");
                this.failure_reason = responseToken[1];
            } else {
                if (responseLine[0].indexOf(WARNING_MESSAGE) != -1) {
                    responseToken = responseLine[0].split(" ");
                    this.warning_message = responseToken[1];
                    activeLine = 1;
                }
                responseToken = responseLine[activeLine++].split(" ");
                this.interval = Integer.parseInt(responseToken[1]);

                responseToken = responseLine[activeLine++].split(" ");
                this.complete = Integer.parseInt(responseToken[1]);

                responseToken = responseLine[activeLine++].split(" ");
                this.incomplete = Integer.parseInt(responseToken[1]);

                responseToken = responseLine[activeLine++].split(" ");
                for (int i = 1; i < responseToken.length; ++i) {
                    responseValue = responseToken[i].split(":");
                    Peer P = new Peer();
                    P.setHostAddr(new InetSocketAddress(responseValue[0], Integer.parseInt(responseValue[1])));
                    this.peers.add(P);
                }
            }
        }
    
    /* getter */
        public String getFailureReason () {
            /* mengembalikan nilai dari failure_reason */
            return (failure_reason);
        }
        public String getWarningMessage () {
            /* mengembalikan nilai dari warning_message */
            return (warning_message);
        }
        public int getInterval () {
            /* mengembalikan nilai dari interval */
            return (interval);
        }
        public int getComplete () {
            /* mengembalikan nilai dari complete */
            return (complete);
        }
        public int getIncomplete () {
            /* mengembalikan nilai dari incomplete */
            return (incomplete);
        }
        public ArrayList < Peer > getPeers () {
            /* mengembalikan nilai dari pees */
            return (peers);
        }
    
    /* method - method yang dimiliki oleh Response */
        public static String fromURLDecoded (String url) throws UnsupportedEncodingException  {
            return (URLDecoder.decode(url, "UTF-8"));
        }
}
